package com.md.api.service;

import com.md.api.common.config.RedisKeys;
import com.md.api.common.utils.*;
import com.md.api.dao.MessageMapper;
import com.md.api.entity.HttpResult;
import com.md.api.entity.SendMail;
import com.md.api.entity.SmsAliCode;
import com.md.api.entity.SysMessage;
import com.md.api.entity.dto.MessageSendSmsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class MessageService {

    @Autowired
    private RedisUtil jRedisUtil;
    @Autowired
    private MessageMapper sysMessageDao;

    public HttpResult sendSms(HttpServletRequest request,MessageSendSmsVo vo) throws Exception {
        String language = request.getHeader("language");
//        Map<String,String> map = new HashMap<>();
        String code = CodeUtil.verifyCode(6);
//        String code = "123456";
//        map.put("code",code);
//        System.out.println("短信驗證碼:"+code);
        //再发一次
//        boolean isSuccess = SmsUtil.sendSms(vo.phone, SmsAliCode.SMS_271570858, map);
//        if(!isSuccess) {
//            return HttpResult.fail("短信驗證碼發送失敗！");
//        }

        jRedisUtil.set(RedisKeys.SMS_CODE.redisKey() + vo.phone, code);
        jRedisUtil.expire(RedisKeys.SMS_CODE.redisKey() + vo.phone, 60*5);

        if(StringUtils.isNotEmpty(language)&&language.equals("en")){
//            SmsUtil.sendmsg(vo.phone,"Your verification code is "+code+" and is valid for 5 minutes. Please do not disclose it.");
            SmsInternationUtils.sendSms(vo.phone,"Your verification code is "+code+" and is valid for 5 minutes. Please do not disclose it.");
        }else{
//            SmsUtil.sendmsg(vo.phone,"您的驗證碼為"+code+"，5分鐘内有效，請勿泄漏。");
            SmsInternationUtils.sendSms(vo.phone,"您的驗證碼為"+code+"，5分鐘内有效，請勿泄漏。");
        }
//        jRedisUtil.set(RedisKeys.SMS_CODE.redisKey() + vo.phone, code);
//        jRedisUtil.expire(RedisKeys.SMS_CODE.redisKey() + vo.phone, 60*5);

        SysMessage message = new SysMessage();
        message.setReceiveUser(vo.phone);
        message.setValidateCode(code);
        message.setContent(code);
        message.setMsgType("phone");
        sysMessageDao.save(message);

        return new HttpResult("短信驗證碼發送成功！");
    }


    public HttpResult sendEmail(SendMail vo) throws Exception {
        String code;
        // 查看是否有缓存
        code = jRedisUtil.getString(RedisKeys.SMS_CODE.redisKey()  + vo.getEmail());
        // 如果没有，生成一个
        if(VerifyUtil.isMiss(code)){
            code= CodeUtil.verifyCode(6);
        }
        vo.setSubject("登錄驗證碼");
        vo.setContent("您本次登錄的驗證碼是："+code+"，請務必注意隱秘保護！");

        try {
            EmailUtil.sendMail(vo);
        }catch (Exception e){
            e.printStackTrace();
            return HttpResult.fail("郵箱發送失敗！");
        }

        jRedisUtil.set(RedisKeys.SMS_CODE.redisKey() + vo.getEmail(), code);
        jRedisUtil.expire(RedisKeys.SMS_CODE.redisKey() + vo.getEmail(), 60*5);

        SysMessage message = new SysMessage();
        message.setReceiveUser(vo.getEmail());
        message.setValidateCode(code);
        message.setContent(code);
        message.setMsgType("email");
        sysMessageDao.save(message);

        return new HttpResult("郵箱發送成功!");
    }

    /**
     * @param vo
     * @return java.lang.String
     */
    public HttpResult smsCheck(MessageSendSmsVo vo) throws Exception {
        String code;
        // 查看是否有缓存
        code = jRedisUtil.getString(RedisKeys.SMS_CODE.redisKey()  + vo.phone);
        if(VerifyUtil.isMiss(code)){
            return HttpResult.fail("驗證碼已過期!");
        }
        if(!code.equalsIgnoreCase(vo.getParamX())){
            return HttpResult.fail("驗證碼錯誤!");
        }
        return HttpResult.OK;
    }


}
